<?php

namespace app\admin\model;

use app\common\enums\StatusEnum;

class Oauth extends \app\common\model\Oauth
{
    /**
     * 通过用户ID获取信息
     * @param array $oauthIds
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getColumnValue($oauthIds = [])
    {
        $sql = static::field('id, nickname');

        if ($oauthIds) {
            $sql->where('id', 'in', $oauthIds);
        }
        $model = $sql->select()
            ->toArray();

        $result = [];
        foreach ($model as $key => $value) {
            $result[$value['id']] = $value;
        }
        return $result;
    }

    /**
     * 列表
     * @param $where
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getList($where)
    {
        $sql = static::field('id, openid,note,communication,class_num, share_key, nickname, phone, device_info,integral, 
        sex, province, city, country, status, create_time,update_time');

        if ($where) {
            $sql->where($where);
        }
        $sql->order('id desc');
        $data = $sql->paginate(null, false, ['query' => request()->param()]);

        $oauthIds = [];
        foreach ($data->items() as $key => $value) {
            $oauthIds[$value['id']] = $value['id'];
        }

        $integralSum = [];
        if ($oauthIds) {
            $integralSum = OauthIntegral::sumIntegral($oauthIds);
        }

        $model = $data->each(function ($item, $key) use ($integralSum) {
            $item['sum'] = isset($integralSum[$item['id']]) ? $integralSum[$item['id']] : 0;
            $item['sex'] = self::attributes('sex', $item['sex']);
            $item['create_time'] = get_datetime($item['create_time']);
            $item['update_time'] = get_datetime($item['update_time']);
            $item['statusText'] = StatusEnum::getStatus($item['status']);
            return $item;
        });

        // 获取总记录数
        $count = $model->total();
        return [
            'model' => $model,
            'count' => $count
        ];
    }

    /**
     * 批量禁用启用
     * @param $ids
     * @param $status
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public static function disables($ids, $status)
    {
        static::where('id', 'in', $ids)->update(['status' => $status]);
    }

    /**
     * 修改状态
     * @param $id
     */
    public static function disabledOne($id)
    {
        $model = static::get($id);
        if ($model) {
            $model->status = ($model->status == StatusEnum::ENABLED) ? StatusEnum::DISABLED : StatusEnum::ENABLED;
            $model->update_time = time();
            $model->save();
        }
    }
    public static function updateset($id)
    {
        $model = static::get($id);

        if ($model) {
            $model->communication = 1;
            $model->save();
        }
    }
    public static function updateNote($id,$note)
    {
        $model = static::get($id);

        if ($model) {
            $model->note = $note;
            $model->save();
        }
    }

    public static function getDetail($id)
    {
        $model = static::get($id);
        if ($model) {
            $integralSum = OauthIntegral::sumIntegral([$id]);
            $model['sum'] = isset($integralSum[$id]) ? $integralSum[$id] : 0;
            $model['sex'] = self::attributes('sex', $model['sex']);
            $model['autoplay'] = self::attributes('autoplay', $model['autoplay']);
            $model['status'] = StatusEnum::getStatus($model['status']);
            $model['create_time'] = get_datetime($model['create_time']);
            $model['update_time'] = get_datetime($model['update_time']);
        }
        return $model;
    }

    public static function getOne($id)
    {
        $model = static::field('id, openid, nickname, phone, integral')
            ->where(['id' => $id])
            ->find();
        return $model ? $model->toArray() : [];
    }

    public static function getId($phone)
    {

        $model = static::
        where(['phone' => $phone])
            ->value('id');

        return $model ? $model : [];
    }

    public static function searchOauth($search)
    {
        return static::field("id, CONCAT_WS(' | ', nickname, phone) AS text, integral")
            ->where(['status' => StatusEnum::ENABLED])
            ->where('nickname', 'like', '%' . $search . '%')
            ->limit(50)
            ->select()
            ->toArray();
    }


    public static function updateStatus($oauthId, $status)
    {
        $user = self::getOne($oauthId);
        if ($user) {
            $user->status = $status;
            return $user->save() ? true : false;
        }
        return false;
    }
}